//
//  Extension+Font.swift
//  ZDKBaseKit
//
//  Created by Killua on 2017/9/28.
//  Copyright © 2017年 JingZhong. All rights reserved.
//

import UIKit

let SizeScale = ScreenWidth != 414 ? 1 : CGFloat(1.2)
let InValidTag = 333

//MARK: -
private func swizzle(_ obj: UIButton.Type) {
    [(#selector(obj.init(coder:)), #selector(obj.kInit(coder:)))].forEach { original, swizzled in
        let originalMethod = class_getInstanceMethod(obj, original)!
        let swizzledMethod = class_getInstanceMethod(obj, swizzled)!

        let didAddViewDidLoadMethod = class_addMethod(obj, original, method_getImplementation(swizzledMethod), method_getTypeEncoding(swizzledMethod))
        if didAddViewDidLoadMethod {
            class_replaceMethod(obj, swizzled, method_getImplementation(originalMethod), method_getTypeEncoding(originalMethod))
        } else {
            method_exchangeImplementations(originalMethod, swizzledMethod)
        }
    }
}

extension UIButton {
    final public class func doBadSwizzleStuff() {
        swizzle(self)
    }

    @objc internal func kInit(coder: NSCoder) -> UIButton  {
        _ = kInit(coder: coder)
        //部分不像改变字体的 把tag值设置成333跳过
        if self.tag != InValidTag {
            let fontSize = self.titleLabel?.font.pointSize
            self.titleLabel?.font = UIFont.systemFont(ofSize: fontSize! * SizeScale)
        }

        return self
    }
}

//MARK: -
private func swizzle(_ obj: UILabel.Type) {
    [(#selector(obj.init(coder:)), #selector(obj.kInit(coder:)))].forEach { original, swizzled in
        let originalMethod = class_getInstanceMethod(obj, original)!
        let swizzledMethod = class_getInstanceMethod(obj, swizzled)!

        let didAddViewDidLoadMethod = class_addMethod(obj, original, method_getImplementation(swizzledMethod), method_getTypeEncoding(swizzledMethod))
        if didAddViewDidLoadMethod {
            class_replaceMethod(obj, swizzled, method_getImplementation(originalMethod), method_getTypeEncoding(originalMethod))
        } else {
            method_exchangeImplementations(originalMethod, swizzledMethod)
        }
    }
}

extension UILabel {
    final public class func doBadSwizzleStuff() {
        swizzle(self)
    }

    @objc internal func kInit(coder: NSCoder) -> UILabel  {
        _ = kInit(coder: coder)
        //部分不像改变字体的 把tag值设置成333跳过
        if self.tag != InValidTag {
            let fontSize = self.font.pointSize
            self.font = UIFont.systemFont(ofSize: fontSize * SizeScale)
        }

        return self
    }
}

//MARK: -
private func swizzle(_ obj: UITextField.Type) {
    [(#selector(obj.init(coder:)), #selector(obj.kInit(coder:)))].forEach { original, swizzled in
        let originalMethod = class_getInstanceMethod(obj, original)!
        let swizzledMethod = class_getInstanceMethod(obj, swizzled)!

        let didAddViewDidLoadMethod = class_addMethod(obj, original, method_getImplementation(swizzledMethod), method_getTypeEncoding(swizzledMethod))
        if didAddViewDidLoadMethod {
            class_replaceMethod(obj, swizzled, method_getImplementation(originalMethod), method_getTypeEncoding(originalMethod))
        } else {
            method_exchangeImplementations(originalMethod, swizzledMethod)
        }
    }
}

extension UITextField {
    final public class func doBadSwizzleStuff() {
        swizzle(self)
    }

    @objc internal func kInit(coder: NSCoder) -> UITextField  {
        _ = kInit(coder: coder)
        //部分不像改变字体的 把tag值设置成333跳过
        if self.tag != InValidTag {
            let fontSize = self.font?.pointSize
            self.font = UIFont.systemFont(ofSize: fontSize! * SizeScale)
        }

        return self
    }
}

//MARK: -
private func swizzle(_ obj: UITextView.Type) {
    [(#selector(obj.init(coder:)), #selector(obj.kInit(coder:)))].forEach { original, swizzled in
        let originalMethod = class_getInstanceMethod(obj, original)!
        let swizzledMethod = class_getInstanceMethod(obj, swizzled)!

        let didAddViewDidLoadMethod = class_addMethod(obj, original, method_getImplementation(swizzledMethod), method_getTypeEncoding(swizzledMethod))
        if didAddViewDidLoadMethod {
            class_replaceMethod(obj, swizzled, method_getImplementation(originalMethod), method_getTypeEncoding(originalMethod))
        } else {
            method_exchangeImplementations(originalMethod, swizzledMethod)
        }
    }
}

extension UITextView {
    final public class func doBadSwizzleStuff() {
        swizzle(self)
    }

    @objc internal func kInit(coder: NSCoder) -> UITextView  {
        _ = kInit(coder: coder)
        //部分不像改变字体的 把tag值设置成333跳过
        if self.tag != InValidTag {
            let fontSize = self.font?.pointSize
            self.font = UIFont.systemFont(ofSize: fontSize! * SizeScale)
        }

        return self
    }
}
